package com.mb.app.appused.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.ListView;

public class CustomListView extends ListView implements OnGestureListener {

	public float scrolledY;
	private Context context = null;
	private boolean outBound = false;
	private int distance;
	private int firstOut;
	GestureDetector lisGestureDetector = new GestureDetector(context, this);

	public CustomListView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context=context;
	}

	public CustomListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context=context;
	}

	public CustomListView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
		this.context=context;
	}

	@SuppressLint("NewApi")
	public CustomListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 添加滚动监听
	 */
	public void addScrollListener(final ScrollListener scroll) {
		this.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {

				}

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

				scrolledY = getPositionY();
				if (scroll != null) {
					scroll.getScrollY(scrolledY);
				}
			}
		});
	}

	float getPositionY() {
		View c = this.getChildAt(0);
		if (c == null) {
			return 0;
		}
		int firstVisiblePosition = this.getFirstVisiblePosition();
		int top = c.getTop();
		return -top + firstVisiblePosition * c.getHeight();
	}

	public interface ScrollListener {
		public void getScrollY(float y);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		int act = event.getAction();
		if ((act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_CANCEL) && outBound) {
			outBound = false;
			// scroll back
		}
		if (!lisGestureDetector.onTouchEvent(event)) {
			outBound = false;
		} else {
			outBound = true;
		}
		Rect rect = new Rect();
		getLocalVisibleRect(rect);
		TranslateAnimation am = new TranslateAnimation(0, 0, -rect.top, 0);
		am.setDuration(300);
		startAnimation(am);
		scrollTo(0, 0);
		return super.dispatchTouchEvent(event);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		// TODO Auto-generated method stub
		int firstPos = getFirstVisiblePosition();
		int lastPos = getLastVisiblePosition();
		int itemCount = getCount();
		// outbound Top
		if (outBound && firstPos != 0 && lastPos != (itemCount - 1)) {
			scrollTo(0, 0);
			return false;
		}
		View firstView = getChildAt(firstPos);
		if (!outBound)
			firstOut = (int) e2.getRawY();
		if (firstView != null && (outBound || (firstPos == 0 && firstView.getTop() == 0 && distanceY < 0))) {
			// Record the length of each slide
			distance = firstOut - (int) e2.getRawY();
			scrollTo(0, distance / 2);
			return true;
		}
		// outbound Bottom
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

}
